package udpInvoker.test.JUnit;

import udpInvoker.main.InvocationMessage;
import udpInvoker.main.Base64;
import udpInvoker.util.UdpUtils;

import java.net.MulticastSocket;
import java.net.DatagramPacket;
import java.net.InetSocketAddress;
import java.net.DatagramSocket;

/**
 * Created by IntelliJ IDEA.
 * User: esr
 * Date: 10.okt.2008
 * Time: 08:51:19
 * Copyright 2008  Espen Skjervold, FFI
 */
public class InvocationTestMessageSender implements Runnable{

    private DatagramSocket multicastSocket;
    private InvocationMessage invocationMessage;
    private boolean useEncryption;
    private String password;

    public InvocationTestMessageSender(DatagramSocket multicastSocket, InvocationMessage invocationMessage, boolean useEncryption, String password) {

        this.multicastSocket = multicastSocket;
        this.invocationMessage = invocationMessage;
        this.useEncryption=useEncryption;
        this.password=password;



        Thread thread = new Thread(this);
        thread.start();
    }


    public void run() {
        try {

            Thread.sleep(1000);

            byte[] buffer = Base64.encodeObject(invocationMessage, Base64.GZIP).getBytes();

            if (useEncryption) {
                buffer = UdpUtils.encryptBuffer(buffer, password);
            }


            DatagramPacket datagramPacket = new DatagramPacket(buffer, buffer.length, new InetSocketAddress("224.224.224.223", 7168));


            multicastSocket.send(datagramPacket);
        } catch (Exception e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
    }




}
